package com.JavaBasic;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.channels.ScatteringByteChannel;
import java.util.Arrays;
import java.util.Scanner;

import com.test.Person;

public class FileOperationDemo {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// 写数据到文件中
		// 参数1 文件路径
		// 参数2 是否是向文件里追加内容
		FileOutputStream fileOutputStream = new FileOutputStream("c.txt",true);
		fileOutputStream.write(97);
		fileOutputStream.close();
		
		// 读文件
		FileInputStream fileInputStream  = new FileInputStream("c.txt");
		System.out.println("读取c.txt文件:");
		int b;
		while((b = fileInputStream.read()) != -1) {
			System.out.println(b);
		}
		fileInputStream.close();
		
		// 拷贝文件
		FileInputStream fi = new FileInputStream("dog.jpg");
		FileOutputStream fo = new FileOutputStream("MyDog.jpg");
		
		// 盛放读取到数据的数组
		byte[] bt = new byte[1024 * 8];
		int d;
		while((d = fi.read(bt)) != -1) {
			fo.write(bt, 0, d);
		}
		fi.close();
		fo.close();
		
		// 用buffer拷贝
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c.txt"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d.txt"));
		int bytes;
		while((bytes = bis.read()) != -1) {
			bos.write(bytes);
		}
		bis.close();
		bos.close();
		
		// 图片加密
		// 123456 相当于文件的密码
		BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("dog.jpg"));
		BufferedOutputStream bos2 = new BufferedOutputStream(new FileOutputStream("1.jpg"));
		int bytes2;
		while((bytes2 = bis2.read()) != -1) {
			bos2.write(bytes2 ^ 123456);
		}
		bis2.close();
		bos2.close();
		
		// 通过制定编码表读写
		InputStreamReader iStreamReader = new InputStreamReader(new FileInputStream("e.txt"), "utf-8");
		OutputStreamWriter wOutputStreamWriter = new OutputStreamWriter(new FileOutputStream("d.txt"),"gbk");
		
		int ch;
		while((ch = iStreamReader.read()) != -1) {
			wOutputStreamWriter.write(ch);
		}
		iStreamReader.close();
		wOutputStreamWriter.close();
		
		// 对象序列化和反序列化
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt"));
		oos.writeObject(new Person("张三", 20));
		ObjectInputStream oisObj = new ObjectInputStream(new FileInputStream("f.txt"));
		System.out.println(oisObj.readObject());
		
		oos.close();
		oisObj.close();
		
		// 键盘录入的两种方式
		// 第一种 Scanner
		System.out.println("请输入:");
		Scanner sc = new Scanner(System.in);
		String string = sc.nextLine();
		
		// 第二种 BufferedReader
		System.out.println("请再次输入:");
		BufferedReader brBufferedReader = 
				new BufferedReader(new InputStreamReader(System.in));
		String string2 = brBufferedReader.readLine();
		if (string.equals(string2)) {
			System.out.println("两次输入一样");
		}else {
			System.out.println("两次输入不同");
		}
		
		// 获取斐波那契数列
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入想要获取的斐波那契数列的大小:");
		int size = scanner.nextInt();
		int[] arr = getFBNQArr(size);
		System.out.println("获取的斐波那契数列为:" + Arrays.toString(arr));
		
	}
	
	// 获取一个斐波那契数列
	// 1，1，2，3，5，8，13，21
	public static int[] getFBNQArr(int size) {
		int[] arr = new int[size];
		
		for(int i = 0; i < arr.length; i++) {
			arr[i] = getNum(i);
		}
		
		return arr;
	}
	
	// 获取斐波那契数列指定索引的数据
	public static int getNum(int index) {
		if (index == 0 || index == 1) return 1;
		
		return getNum(index - 1) + getNum(index - 2);
	}
}
